//$('form').submit(function() {
//	alert(123)
//  alert($(this).serialize());
//  return false;
//});
 


			
			

$(function(){
	
	//将form表单转换为json数据
	$.fn.serializeJson=function(){  
        var serializeObj={};  
        var array=this.serializeArray(); //将form表单序列化数组对象 
        //console.log("array="+ JSON.stringify(array));
        //var str=this.serialize();  //将form表单序列化字符串
        $(array).each(function(){  //遍历表单数组拼接json串
            if(serializeObj[this.name]){
//            	debugger;
                if($.isArray(serializeObj[this.name])){  
                    serializeObj[this.name].push(this.value);  
                }else{  
                    serializeObj[this.name]=[serializeObj[this.name],this.value];
                }  
            }else{  
                serializeObj[this.name]=this.value;   
            }  
        });  
        return serializeObj;  
	};
	
	

	$('#g').on("click",function(){
		//console.log('$("#dataform").serialize()='+JSON.stringify($("#dataform").serialize()));
		//console.log('$("#dataform").serializeJson()='+JSON.stringify($("#dataform").serializeJson()));
		var jsonData = $("#dataform").serializeJson();
		//console.log(JSON.stringify(arrayJsonToStringJson(jsonData)));
		
		$.ajax({
			type:"POST",
			url:ctx+ (($("#id").val()=="")? "/student/add" : "/student/update"),
			contentType: 'application/json',
			dataType : 'json',
			data:JSON.stringify(arrayJsonToStringJson(jsonData)),
//			data:$("#dataform").serialize(),
			success:function(data){
				alert(JSON.stringify(data));
//				alert('success');
			},error:function(data){
				alert("false");
			}
		});
	});
	
	
	/*
	var serializeObj={};
	serializeObj['age']=30
	console.log(JSON.stringify(serializeObj));
	
	//添加一个属性 name:value 
	serializeObj['interest']='1';
	console.log(JSON.stringify(serializeObj));
	
	// 将元素变为 数组 ，interest : ['1','2'] 
	serializeObj['interest']=[serializeObj['interest'],'2'];
	console.log(JSON.stringify(serializeObj));
	
	// 追加一个值 interest : ['1','2','3'] 
	serializeObj['interest'].push('3');
	console.log(JSON.stringify(serializeObj));
	*/
	
	$("input[name='interest_show']").each(function(i,elemt){
//	    $(this).change(function(){
//	            var ischecked = $(this).prop("checked");
//	            alert($(this).val());
//	            //$(this).siblings('input').prop("checked",ischecked);
//	       });
		
		$(this).change(function(){
			var interestData = "";
			$("input[name='interest_show']").each(function(){
				
				if($(this).prop("checked")){
					//alert($(this).val());
					interestData +=$(this).val()+",";
				}
			});
			$("#interest").val(interestData.substring(0,interestData.length-1));
		});
	})
	
	
});


//含数组的json数据变为string类型的json数据
//将数据{"name":"张三5","age":"81","c":"3","d":"4","education":"3","interest":["2","3"]}
//变为 {"name":"张三5","age":"81","c":"3","d":"4","education":"3","interest":"2,3"}
function arrayJsonToStringJson(jsonData){
	$.each(jsonData, function(i, val){
		if($.isArray(jsonData[i])){
			var arrayData = jsonData[i];
			jsonData[i] = arrayToString(arrayData);
			
		}
	})
	return jsonData;
}

//数组转为string，逗号分隔
function arrayToString(arrayData){
	var returnString="";
	$(arrayData).each(function(index, value){
		returnString += (value+",");
	})
	return returnString.substring(0,returnString.length-1);
}
